﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Security.Cryptography;

namespace mysql_db_stress_test_cs
{
    class Program
    {
        static MySqlConnection conn;
        static MySqlCommand com;
        static void createdb()
        {
            
            Program.com.CommandText = "CREATE DATABASE tmp123";
            Program.com.ExecuteNonQuery();
        }
        static void createtable()
        {
            Program.com.CommandText = "USE tmp123";
            Program.com.ExecuteNonQuery();
            Program.com.CommandText = "CREATE TABLE usertest (`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,`username` CHAR(32) NOT NULL,`passwd` CHAR(32) NOT NULL,`email` CHAR(100) NOT NULL,`hash` CHAR(32) NOT NULL,PRIMARY KEY (`id`)  )";
            Program.com.ExecuteNonQuery();
        }
        static void insertrows()
        {
            Random rnd = new Random();
            int rndpass;
            String strhash;
            String tohash;
            String stri;
            MD5 md5 = MD5.Create();
            byte[] inputBytes;
            byte[] hash;
            StringBuilder sb;
            for(int i = 1; i < 90001; i++)
            {
                rndpass = rnd.Next(111,999);
                stri = i.ToString();
                tohash = "example" + stri + rndpass.ToString() + "example" + stri + "@example.com";
                inputBytes = Encoding.ASCII.GetBytes(tohash);
                hash = md5.ComputeHash(inputBytes);
                sb = new StringBuilder();
                for (int s = 0; s < hash.Length; s++)
                    sb.Append(hash[s].ToString("X2"));
                strhash = sb.ToString();
                Program.com.CommandText = "INSERT INTO usertest (`id`,`username`,`passwd`,`email`,`hash`) VALUES ( NULL,'example" + stri + "','" + rndpass.ToString() + "','example" + stri + "@example.com','" + strhash + "')";
                Program.com.ExecuteNonQuery();
            }

        }
        static void selectrows()
        {
            MySqlDataReader Reader;
            Program.com.CommandText = "SELECT * FROM usertest";
            Reader = Program.com.ExecuteReader();
            MD5 md5 = MD5.Create();
            byte[] inputBytes;
            byte[] hash;
            StringBuilder sb;
            while (Reader.Read())
            {
                inputBytes = Encoding.ASCII.GetBytes(Reader.GetValue(1).ToString()+Reader.GetValue(2).ToString()+Reader.GetValue(3).ToString());
                hash = md5.ComputeHash(inputBytes);
                sb = new StringBuilder();
                for (int i = 0; i < hash.Length; i++)
                    sb.Append(hash[i].ToString("X2"));
                if (sb.ToString() != Reader.GetValue(4).ToString())
                    Console.WriteLine("Something went wrong!");
            }
            Reader.Close();
        }
        static void deleterows()
        {
            Program.com.CommandText = "DELETE FROM usertest";
            Program.com.ExecuteNonQuery();
        }
        static void deletetable()
        {
            Program.com.CommandText = "DROP TABLE usertest";
            Program.com.ExecuteNonQuery();
        }
        static void deletedb()
        {
            Program.com.CommandText = "DROP DATABASE tmp123";
            Program.com.ExecuteNonQuery();
        }
        static void Main(string[] args)
        {
            string constring = "SERVER=localhost;" +
                "UID=root;" +
                "PASSWORD=root;";
            Program.conn = new MySqlConnection(constring);
            Program.com = conn.CreateCommand();
            conn.Open();
            Program.createdb();
            Program.createtable();
            Program.insertrows();
            Program.selectrows();
            Program.deleterows();
            Program.deletetable();
            Program.deletedb();
            conn.Close();
        }
    }
}
